#!/bin/bash

EE_FILE_NOT_FOUND=1

function subs_project_name {
	file="$1"
	if ! [ -e $file ]
	then
		echo "initproject: error: file $file not found. Substitution failed."
		exit $EE_FILE_NOT_FOUND
	fi
	tmpfile="$file".tmp
	sed "s/Primes/$projname/" $file > $tmpfile
	rm $file
	mv $tmpfile $file
}

#edit Project Name string in these files
if [ -z "$1" ]
then
	echo "initproject: Init a UVa project for CS 371p. Should run at the project dir."
	echo "Usage: ./utils/initproject [projectname]"
	echo "       or edit project name in Makefile, then \"make init\""
	exit 0
fi

projname="$1"

if [ "$projname" == "Primes" ]
then
	echo "No need to change project name. The default is \"Primes\"."
	exit 0
fi

#do string substitution
files=( "Makefile" "Primes.in" "TestPrimes.h" "Doxyfile" "main.c++" )

count=${#files[*]}

#echo "substitute Primes with $projname in $count files."
for (( i = 0 ; i < $count ; i++ ))
do
    subs_project_name ${files[$i]}
done

#a few renames
mv TestPrimes.h Test$projname.h
mv Primes.in $projname.in
